//支付宝跳转到支付页面
function alipay_pay(tbl) {
	var conf=$fetch("conf","id=1")
	var alipay_appid=conf.alipay_appid;
	var alipay_private_key=conf.alipay_private_key;
	var alipay_public_key=conf.alipay_public_key;
	var order=dao.fetch("orders",Cnd.where("id","=",tbl.id))
	//获得初始化的AlipayClient(//gatewayUrl//appid//私钥//文档类型//charset//公钥//sign_type)
	var alipayClient = new com.alipay.api.DefaultAlipayClient("https://openapi.alipay.com/gateway.do",alipay_appid,alipay_private_key,"json","utf-8",alipay_public_key,"RSA2");
	var alipayRequest = new com.alipay.api.request.AlipayTradePagePayRequest();//设置请求参数
	alipayRequest.setReturnUrl("http://www.iepsy.com/pay/alipay_return");
	alipayRequest.setNotifyUrl("http://www.iepsy.com/pay/alipay_notify");
	alipayRequest.setBizContent("{\"out_trade_no\":\""+ order.orderno +"\"," 
			+ "\"total_amount\":\""+ order.price +"\"," 
			+ "\"subject\":\""+ order.title +"\"," 
			+ "\"body\":\""+ order.title +"\"," 
			+ "\"order_id\":\""+ order.id +"\"," 
			+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
	var result = alipayClient.pageExecute(alipayRequest).getBody();//请求
	return result;
}
function alipay_return(tbl){
	try {
	    Thread.sleep(5000);                 //1000 毫秒，也就是1秒.
	} catch(e) {
	    
	}
	return "http://www.iepsy.com/users/orders"
}
//通知
function alipay_notify(tbl){
	var conf=$fetch("conf","id=1")
	//商户订单号
	var out_trade_no = tbl.out_trade_no;
	//支付宝交易号
	var trade_no = tbl.trade_no;
	//交易状态
	var trade_status = tbl.trade_status;
	var verify_result=false;
	try {
		verify_result = com.alipay.api.internal.util.AlipaySignature.rsaCheckV1(tbl, conf.alipay_public_key, "utf-8", "RSA2");
	} catch (e) {
		print("conver alipay order fail!")
	}
	var result=""
	if(verify_result){//验证成功
		if(trade_status=="TRADE_FINISHED"||trade_status=="TRADE_SUCCESS"){
			
			var order = dao.fetch("orders", Cnd.where("orderno", "=", out_trade_no));
			if(order.status == 1){
				result= "order ok!";
				return result;
			}
			if (order.price != tbl.total_fee) {
				result= "total_fee error!"
			}
			if(trade_status=="TRADE_FINISHED"){
				print("waring!重新付款得！");
			}
			dao.update("orders",Chain.make("status",1).add("pay_type",1),Cnd.where("id","=",order.id));
		}
		result="success"
	}else{//验证失败
		result= "fail";
	}
	var tab=new Record();
	tab.set("id", uuid());
	tab.set(".table", "alipay");
	tab.set("is_success",tbl.is_success)
	tab.set("notify_id",tbl.notify_id)
	tab.set("notify_time",tbl.notify_time)
	tab.set("notify_type",tbl.notify_type)
	tab.set("out_trade_no",tbl.out_trade_no)
	tab.set("payment_type",tbl.payment_type)
	tab.set("seller_id",tbl.seller_id)
	tab.set("service",tbl.service)
	tab.set("subject",tbl.subject)
	tab.set("total_fee",tbl.total_fee)
	tab.set("trade_no",tbl.trade_no)
	tab.set("trade_status",tbl.trade_status)
	tab.set("sign",tbl.sign)
	tab.set("sign_type",tbl.sign_type)
	tab.set("buyer_email",tbl.buyer_email)
	tab.set("buyer_id",tbl.buyer_id)
	tab.set("exterface",tbl.exterface)
	tab.set("seller_email",tbl.seller_email)
	tab.set("discount",tbl.discount)
	tab.set("gmt_create",tbl.gmt_create)
	dao.insert(tab);
	return result;	
}
